package chances.wechat.gateway;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import chances.framework.ufs.UserFileSystem;
import chances.wechat.controller.UserAbstractController;
import chances.wechat.data.PageBean;
import chances.wechat.entity.UserMedia;
import chances.wechat.resp.ResultResponse;
import chances.wechat.resp.ResultSetResponse;
import chances.wechat.service.UserMediaService;
import chances.wechat.utils.RequestUtils;

/*@RestController
@RequestMapping(path = "/photo")*/
public class PhotoController extends UserAbstractController {

	Logger logger = LoggerFactory.getLogger(PhotoController.class);
	
	@Autowired
	UserMediaService userMediaService;
	
	@RequestMapping(value = "/list")
	public ResultSetResponse<Object> list(HttpServletRequest request) throws Exception {
		
		String userId = RequestUtils.getParameter(request, "userId", "");
		int size = RequestUtils.getParameter(request, "pageSize", 10);
		int pageIndex = RequestUtils.getParameter(request, "pageIndex", 1);
		int status = RequestUtils.getParameter(request, "status", UserMedia.PHOTO_PASS);
		ResultSetResponse<Object> result = new ResultSetResponse<Object>();
		result.setStatus(SUCCESS);
		UserMedia userMediaParam = new UserMedia();
		userMediaParam.setOpenId(userId);
		userMediaParam.setStatus(status);
		
		try {
			List<Object> date = this.getDate(request, userMediaParam, pageIndex, size);
			if(date != null) {
				result.setResultSet(date);
				result.setPageBean(this.getUserMediaPageBean(request, userMediaParam, size, pageIndex));
			}
		} catch (Exception e) {
			result.setStatus(SYS_ERROR);
		}
		
		return result;
	}
	
	public PageBean getUserMediaPageBean(HttpServletRequest request, UserMedia userMediaParam, int size, int pageIndex) {
		PageBean result = this.getPageBean(request);
		int totalCount = this.userMediaService.getTotalCount(userMediaParam);
		result.setPageCount((int) ((totalCount - totalCount % size) / size) + 1);
		result.setCurrentPage(pageIndex);
		return result;
	}
	
	protected List<Object> getDate(HttpServletRequest request, UserMedia userMediaParam, int pageIndex, int size) {
		
		PageBean pageBean = this.getPageBean(request);
		List<Object> result = this.userMediaService.listUserMedia(userMediaParam, 0, pageBean);
		for(Object temp:result) {
			UserMedia userMedia = (UserMedia)temp;
			String path = UserFileSystem.getHashPath(userMedia.getOpenId());
			String photoUrl = this.userMediaService.getPhotoUrl();
			String basePath = photoUrl+path+"photo/";
			//System.out.println("path:"+path+",photoUrl:"+photoUrl);
			userMedia.setIcon(basePath + "icon_" + userMedia.getMediaId() + ".jpg");
			userMedia.setHd(basePath + "hd_" + userMedia.getMediaId() + ".jpg");
			userMedia.setSd(basePath + "sd_" + userMedia.getMediaId() + ".jpg");
		}
		
		return result;
	}
	
	@RequestMapping("/remove")
	public ResultResponse<String> remove(HttpServletRequest request) {
		String mediaId = RequestUtils.getParameter(request, "mediaId", "");
		ResultResponse<String> res = new ResultResponse<String>();
		res.setStatus(SUCCESS);
		try {
			UserMedia usermedia = this.userMediaService.getMediaByMediaId(mediaId);
			usermedia.setStatus(UserMedia.PHOTO_DELETE);
			this.userMediaService.updateMedia(usermedia);
			res.setResult(mediaId);
		} catch (Exception e) {
			res.setStatus(SYS_ERROR);
		}
		return res;
	}
	
	@RequestMapping("/dels")
	public ResultSetResponse<String> delMediaByMediaIds(HttpServletRequest request) {
		String mediaIds = RequestUtils.getParameter(request, "mediaIds", "");
		ResultSetResponse<String> res = new ResultSetResponse<String>();
		res.setStatus(SUCCESS);
		try {
			if (StringUtils.isNotEmpty(mediaIds)) {
				String[] ids = mediaIds.split(",");
				this.userMediaService.delMediaByMediaIds(ids, UserMedia.PHOTO_DELETE);
			}
		} catch (Exception e) {
			res.setStatus(SYS_ERROR);
		}
		return res;
	}

}
